package com.recipedb.recipedb.controller;

import com.recipedb.recipedb.entity.UserMessage;
import com.recipedb.recipedb.service.UserMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/usermessages")
public class UserMessageController {

    @Autowired
    private UserMessageService service;

    @PostMapping
    public ResponseEntity<UserMessage> create(@RequestBody UserMessage entity) {
        return ResponseEntity.ok(service.save(entity));
    }

    @GetMapping
    public ResponseEntity<List<UserMessage>> getAll() {
        return ResponseEntity.ok(service.findAll());
    }

    @GetMapping("/{id}")
    public ResponseEntity<Optional<UserMessage>> getById(@PathVariable Long id) {
        return ResponseEntity.ok(service.findById(id));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        service.deleteById(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/user/{userId}")
    public List<UserMessage> getUserMessages(@PathVariable Long userId) {
        return service.findByUserId(userId);
    }

    @GetMapping("/user/{userId}/unread")
    public List<UserMessage> getUnreadMessages(@PathVariable Long userId) {
        return service.findUnreadByUserId(userId);
    }

    @PutMapping("/user/{userId}/read-all")
    public ResponseEntity<?> markAllAsRead(@PathVariable Long userId) {
        service.markAllAsRead(userId);
        return ResponseEntity.ok().build();
    }
}
